
const gulp = require('gulp');

const autoprefixer = require('gulp-autoprefixer');
const cssmin = require('gulp-cssmin');

const del = require('del');

const uglify = require('gulp-uglify');
const babel = require('gulp-babel');

const htmlMin = require('gulp-htmlmin');

const cssHandler = function(){
    return gulp.src('./src/assets/css/*.css').pipe(autoprefixer()).pipe(cssmin()).pipe(gulp.dest('./dist/css'));
}

const jsHandler = function(){
    return gulp.src('./src/assets/js/*.js').pipe(babel({presets:['@babel/env']})).pipe(uglify()).pipe(gulp.dest('./dist/js'));
}

const htmlHandler = function(){
    return gulp.src('./src/page/*.html').pipe(htmlMin({
        removeAttributeQuotes:true,
        removeComments:true,
        removeEmptyAttributes:true,
        removeScriptTypeAttributes:true,
        removeStyleLinkTypeAttributes:true,
        collapseBooleanAttributes:true,
        collapseWhitespace:true,
        minifyCSS:true,
        minifyJS:true
    })).pipe(gulp.dest('./dist/pages'));
}

// const imagesHandler1 = function(){
//     return gulp.src('./src/images/*.*').pipe(gulp.dest('./dist/images'));
// }
// const imagesHandler2 = function(){
//     return gulp.src('./src/pages/xiaomi-images/*.*').pipe(gulp.dest('./dist/images'));
// }

// const phpHandler = function(){
//     return gulp.src('./src/server/*.php').pipe(gulp.dest('./dist/server'));
// }

// const toolsHandler = function(){
//     return gulp.src('./src/tools/**').pipe(gulp.dest('./dist/tools'));
// }

const delHandler = function(){
    return del(['./dist']);
}


const watchHandler =function(){
    gulp.watch('./src/assets/css/*.css',cssHandler);
    gulp.watch('./src/assets/js/*.js',jsHandler);
    gulp.watch('./src/pages/*.html',htmlHandler);
    // gulp.watch('./src/images/*.*',imagesHandler1);
    // gulp.watch('./src/pages/xiaomi-images/*.*',imagesHandler2);
    // gulp.watch('./src/server/*.php',phpHandler);
    // gulp.watch('./src/tools/**',toolsHandler)
}



module.exports.default = gulp.series(
    delHandler,
    gulp.parallel(cssHandler,jsHandler,htmlHandler),
    watchHandler
)